package org.example.dingding;

import com.alibaba.fastjson2.JSONObject;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

@Slf4j
public class DingDemo {

    public static final String appKey = "dingzqktuvbpb6f0bcg4";
    public static final String appSecret = "YNfL_sA6bMX2w0AHydJFQFclywBXOgh3W2R0NWChrAoLXmXUICBNV1gb2y9PoPkh";
    public static final String robotCode = "dingzqktuvbpb6f0bcg4";
    public static final String mobile = "18336350982";

    public static com.aliyun.dingtalkoauth2_1_0.Client createAccessTokenClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }


    public static com.aliyun.dingtalkrobot_1_0.Client createSendMsgClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkrobot_1_0.Client(config);
    }



    public static void main(String[] args)   {
        try {
            /**
             * 这里使用了新版sdk，获取accessToken
             */
            com.aliyun.dingtalkoauth2_1_0.Client accessTokenClient = DingDemo.createAccessTokenClient();
            com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest accessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest();
            accessTokenRequest .setAppKey(appKey)
                    .setAppSecret(appSecret);
            GetAccessTokenResponse accessTokenResponse = accessTokenClient.getAccessToken(accessTokenRequest);
            String accessTokenStr = accessTokenResponse.getBody().getAccessToken();
            log.info("accessToken:{}",accessTokenStr);

            /**
             * 这里使用了旧版SDK，根据手机号获取用户的userid，需要在应用的权限控制里开通“通过手机号获取用户id”接口的权限，需要管理员在OA里审批通过
             */
            DingTalkClient userIdClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
            OapiV2UserGetbymobileRequest userIdRequest = new OapiV2UserGetbymobileRequest();
            userIdRequest.setMobile(mobile);
            OapiV2UserGetbymobileResponse userIdResponse = userIdClient.execute(userIdRequest, accessTokenStr);
            String userId = userIdResponse.getResult().getUserid();
            log.info("userId：{}",userId);

            /**
             * 这里使用了新版sdk，根据userid、机器人code发送消息
             */
            com.aliyun.dingtalkrobot_1_0.Client sendMsgClient = DingDemo.createSendMsgClient();
            com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders batchSendOTOHeaders = new com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders();
            batchSendOTOHeaders.xAcsDingtalkAccessToken = accessTokenStr;
            JSONObject msgParam = new JSONObject();
            /*msgParam.put("title","这是标题");
            msgParam.put("text","这是内容这是内容这是内容这是内容这是内容");*/
            msgParam.put("content","这是内容这是内容这是内容这是内容这是内容");
            com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest batchSendOTORequest = new com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest()
                    .setMsgParam(msgParam.toJSONString())
//                    .setMsgKey("sampleMarkdown")
                    .setMsgKey("sampleText")
                    .setRobotCode(robotCode)
                    .setUserIds(Arrays.asList(
                            userId
                    ));
            BatchSendOTOResponse batchSendOTOResponse = sendMsgClient.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions());
            log.info("无效的用户userId列表：{}",batchSendOTOResponse.getBody().getInvalidStaffIdList());
            log.info("被限流的userId列表：{}",batchSendOTOResponse.getBody().getFlowControlledStaffIdList());
            log.info("发送的消息的id，可用于获取消息是否已读和撤回等功能：{}",batchSendOTOResponse.getBody().getProcessQueryKey());
        }catch (Exception exception) {
            log.error("{}",exception.getMessage());
        }


    }
}
